package com.permission.guochaodev

import android.content.pm.PackageManager
import androidx.fragment.app.Fragment

/**
 *@fileName InvisibleFragment
 *@date2020/12/3  16:26
 *@author InvisibleFragment$
 *@describe TODO
 *@desc
 */
typealias PermissionCallback=(Boolean,List<String>)->Unit
//使用typealias可以将任意类型的数据起一个别名
class InvisibleFragment:Fragment() {
    private var callback:PermissionCallback?=null

    fun requestNow(cb:PermissionCallback,vararg permissions:String){
        callback=cb
        requestPermissions(permissions,1)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray) {
        if(requestCode==1){
            val deniedList=ArrayList<String>()
            for ((index,result) in grantResults.withIndex()){
                if(result!=PackageManager.PERMISSION_GRANTED){
                    deniedList.add(permissions[index])
                }
            }
            val allGranted=deniedList.isEmpty()
            callback?.let { it(allGranted,deniedList) }
        }
    }

}